$(function()
{
    var width = $(document).width();
    var length = $('.review_item').length;
    var column = width <= 1200 ? (width > 800 ? 2 : 1) : 3;
    var column_index = [];
    var toggle = true;
    var $page,
        $total_pages,
        $count = 0;
    if (column_index.length == 0)
    {
        for (let i = 1; i <= column; i++)
        {
            column_index.push(i)
        }
    }
    grid_view();
    $(window).scroll(function()
    {
        let top = $(window).scrollTop();
        let height = $('.reviews_box').height();
        if (height - top < 380)
        {
            get_reviews()
        }
    });
    function get_reviews()
    {
        $page = $('.reviews_box').find('input[name=page').val();
        $total_pages = $('.reviews_box').find('input[name=total_pages').val();
        $count = $('.reviews_box').find('input[name=count').val();
        if ($('.review_item').length < $count)
        {
            $page = 1;
            $count = 0
        }
        if ($page < $total_pages && toggle)
        {
            $('.reviews .loding div').css('display', 'block');
            toggle = false;
            $.ajax({
                url: './?do_action=action.get_review_view',
                dataType: 'json',
                data: {
                    page: $page,
                    total_pages: $total_pages,
                    count: $count
                },
                success: function(data)
                {
                    if (data.ret == 1)
                    {
                        $('.reviews_box').append(data.msg.html);
                        let article_rows = data.msg.article_rows;
                        $('.reviews_box').find('input[name=page').val(article_rows[2]);
                        $('.reviews_box').find('input[name=total_pages').val(article_rows[3]);
                        $('.reviews_box').find('input[name=count').val(article_rows[4]);
                        grid_view();
                        if (article_rows[2] < article_rows[3])
                        {
                            toggle = true
                        }
                        else
                        {
                            $('.reviews .loding').css('display', 'none')
                        }
                        $('.reviews .loding div').css('display', 'none')
                    }
                }
            })
        }
    }
    function grid_view()
    {
        $('.review_item').css('width', 'calc(100% / ' + column + ')');
        $('.review_item').css('max-width', 'calc(100% / ' + column + ')');
        if (length > column)
        {
            $(".review_item").each(function()
            {
                let index = parseInt($(this).attr("rel"));
                count = $('.reviews_box').find('input[name=count').val();
                if (index > count)
                {
                    if (index > column)
                    {
                        let height = 0;
                        let add_index = index - column;
                        let add_index_height = 1000000;
                        let add_index_left = 0;
                        column_index.forEach(e => 
                            {
                                height = get_position($(".review_item[rel=" + e + "]"));
                                if (height < add_index_height)
                                {
                                    add_index_height = height;
                                    add_index = e;
                                    add_index_left = get_position($(".review_item[rel=" + e + "]"), 'left')
                                }
                            });
                        for (let j = 0; j < column_index.length; j++)
                        {
                            if (column_index[j] == add_index)
                            {
                                column_index[j] = index
                            }
                        }
                        $(this).css("top", add_index_height);
                        $(this).css("left", add_index_left)
                    }
                    else
                    {
                        $(this).css("top", 0);
                        if (index == 1)
                        {
                            $(this).css("left", 0)
                        }
                        else if (index == 2)
                        {
                            $(this).css("left", $(".review_item[rel=1]").outerWidth())
                        }
                        else
                        {
                            $(this).css("left", get_position($(".review_item[rel=2]"), 'left') + $(".review_item[rel=2]").outerWidth())
                        }
                    }
                }
            })
        }
        let max_height = 0;
        column_index.forEach(e => 
            {
                let height = get_position($(".review_item[rel=" + e + "]"));
                if (height > max_height)
                {
                    max_height = height
                }
            });
        $(".reviews_box").height(max_height);
        return true
    }
    function get_position(node, type = 'height')
    {
        if (node.length > 0)
        {
            if (type == 'height')
                return node.position().top + node.outerHeight();
            if (type == 'left')
                return node.position().left
        }
        return 0
    }
});
